package com.hinnotek.warehouse.viewmodel

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Transformations
import com.google.gson.ExclusionStrategy
import com.google.gson.FieldAttributes
import com.google.gson.Gson
import com.google.gson.GsonBuilder
import com.hinnotek.warehouse.bean.WarehourseApplyFormDevices
import com.hinnotek.warehouse.bean.WarehourseApplyFormDevicesProjects
import com.lee.common.network.getRequestBody
import com.lee.common.utils.SharedPreUtils
import dagger.hilt.android.lifecycle.HiltViewModel
import javax.inject.Inject


/**
 * 新增 入库/出库/借用/报损单
 */
@HiltViewModel
class NewlyAddedModel @Inject constructor() :
    BaseViewModel() {

    var wareId = 0          //仓库Id
    var wareName = ""       //仓库名称
    var approval = ""       //审批人
    var approvalUserId = 0  //审批人用户id
    var sendUser = ""       //抄送人
    var sendUserId = 0      //抄送人用户id
    var projectRemarks = "" //备注
    var applyType = "00"    //01入库单 02出库单 03借用单 04报损单 05预出库
    var goodsType = "01"    //01-成品；02-半成品；03-原材料
    val sendData = MutableLiveData<Boolean>()
    val wareData = MutableLiveData<Boolean>()

    //新增List数据
    var addFormList = mutableListOf<WarehourseApplyFormDevices>()


    private val warehouse = Transformations.switchMap(refreshTrigger) {
        api.getSysWarehouseListByUserId(SharedPreUtils.getInteger("userId",0))
    }

    private val sendUserList = Transformations.switchMap(refreshTrigger) {
        api.getSendUserList()
    }

    private val materielNum = Transformations.switchMap(wareData) {
        api.getMaterielNumList(wareId)
    }

    private val approvalUser = Transformations.switchMap(refreshTrigger) {
        api.getApprovalUserList()
    }

    private val addWarehourseApplyForm = Transformations.switchMap(sendData) {
        val map = mutableMapOf<String, Any>().apply {
            //01-入库单；02-出库单；03-借用单；04-报损单
            put("applyType", applyType)
            //货物类别：01-成品；02-半成品；03-原材料
            put("goodsType", goodsType)
            //申请人用户id
            val userName = SharedPreUtils.getString("personName")
            val userId = SharedPreUtils.getInteger("userId", -1)
            put("applyUserId", userId)
            put("applyUser", userName)
            // 申请人类别：1-销售；2-技术；3-研发 4仓库管理
            put("applyUserType", 4)
            //仓库
            put("wareId", wareId)
            put("wareName", wareName)
            //抄送
            put("sendUser", sendUser)
            put("sendUserId", sendUserId)
            //审批
            put("approvalUser", approval)
            put("approvalUserId", approvalUserId)

            put("projectRemarks", projectRemarks)
            //添加
            val jsonData =
                GsonBuilder().addSerializationExclusionStrategy(MyExclusionStrategy()).create()
                    .toJson(WarehourseApplyFormDevicesProjects(addFormList))
            put("warehourseApplyFormDeviceJson", jsonData)
        }
        api.addWarehourseApplyForm(getRequestBody(map))
    }

    val warehousePage = mapPage(warehouse)

    val sendUserPage = mapPage(sendUserList)

    val materielNumPage = mapPage(materielNum)

    val approvalUserPage = mapPage(approvalUser)

    val addWarehourseApplyFormPage = mapData(addWarehourseApplyForm)

    fun send() {
        sendData.value = true
        loading.value = true
    }

    fun ware() {
        wareData.value = true
        loading.value = true
    }
}

class MyExclusionStrategy : ExclusionStrategy {
    override fun shouldSkipField(f: FieldAttributes?): Boolean {
        if (f?.name == "index") {
            return true; //过滤掉index字段
        }
        return false
    }

    override fun shouldSkipClass(clazz: Class<*>?): Boolean {
        return false;
    }
}
